
************************************************
*** Table on provisions and ETRs	     ***
************************************************

*Read
u "$cur_sample" if inlist(year,2016,2019), clear
do $cleaning/renameSOI.do 

*Keep ETR firms 	
keep if pidom_soi>0

*Set panel 
gegen t = group(year)
tsset ein t

*ETR 
g etr = uscetr_soi*100 
g letr = ln(1 + etr)
g detr = d.etr
g dletr = d.letr

*Keep only 2016 + the ETR change 
foreach i in detr dletr {
	gegen x = max(`i'), by(ein)
	replace `i' = x 
	drop x
}
keep if year==2016

*Reconstruct a couple variables
g etr_post = etr+detr
g payroll = cost_of_labor + salaries_wages_nde + compensation_officers

*Winsorize ETR change 
winsor2 detr, cuts(5 95) replace
winsor2 etr, cuts(0 95) replace
winsor2 etr_post, cuts(0 95) replace

/*******************************************************************************
Begin Summary stats table
*******************************************************************************/
est clear

global SUMM sale total_assets depreciable_assets payroll net_income tax

foreach v of varlist $SUMM {
	replace `v'=`v'/1e3
}

*Recreate variable labels
label var sale "Gross Receipts (000s)"
label var total_assets "Total Assets (000s)"
label var depreciable_assets "Depreciable Assets (000s)"
label var payroll "Payroll (000s)"
label var net_income "Net Income (000s)"
label var tax "Tax Paid (000s)"
label var etr "ETR in 2016"
label var etr_post "ETR in 2019"

preserve

	keep if year==2016
	gegen nfirms = total(weightSOI), by(firmtype_alt)
	label var nfirms "Unique Firms"

	gcollapse $SUMM emp_w2 etr etr_post nfirms [aw=weightSOI], by(firmtype_alt)
	format $SUMM emp_w2 nfirms %10.0fc
	format etr* %9.1f
	tostring *, force replace u
	sxpose, force clear firstnames 

	g row = ""
	replace row = "Gross Receipts (000s)" in 1 
	replace row = "Total Assets (000s)" in 2 
	replace row = "Depreciable Assets (000s)" in 3 
	replace row = "Payroll (000s)" in 4 
	replace row = "Net Income (000s)" in 5 
	replace row = "Tax Paid (000s)" in 6 
	replace row = "Employment" in 7 
	replace row = "ETR in 2016" in 8 
	replace row = "ETR in 2019" in 9 
	replace row = "Unique Firms" in 10

	*Format
	order row *
	replace _var5 = _var5 + " \\" if _n<10

	*Export to TeX
	listtex * using "$results/table1a.tex", replace ///
		rstyle(tabular) ///
		head("\begin{tabular}{l c c c c c c}" /// number of L or R justified columns in the table
		"\toprule" /// thick line at top of the table
		"       & (1)   		& (2)		& (3)		& (4)           & (5)  \\" ///
		" 	& Public        	& Public   	& Private       & Large Priv  	& Small Priv \\" ///
		" 	& Multinational 	& Domestic 	& Multinational & Domestic 	& Domestic \\ \midrule") /// 
			foot(   "\bottomrule" /// /// thick line at bottom of the table 
		"\end{tabular}")

restore

/*******************************************************************************
Summary Stats shares table by firmtype_alt
*******************************************************************************/ 
preserve 
	keep if year==2016
	g n = 1 
	replace weightSOI = round(weightSOI)
	foreach v in n sale tax emp_w2 depreciable_assets {
		cap drop `v'_tot `v'_type `v'_share
		gegen `v'_tot = sum(`v') [fw=weightSOI]
		gegen `v'_type = sum(`v') [fw=weightSOI], by(firmtype_alt)
		g `v'_share = `v'_type / `v'_tot
	}

	collapse *_share, by(firmtype_alt)
	xpose, clear varname 
	drop in 1 
	g row = "\# Firms" if _varname=="n_share"
	replace row = "Gross Receipts" if _varname=="sale_share"
	replace row = "Tax Paid" if _varname=="tax_share"
	replace row = "Employment" if _varname=="emp_w2_share" 
	replace row = "Depreciable Assets" if _varname=="depreciable_assets_share" 

	drop _varname 
	order row v* 
	format v* %9.3f
	tostring v*, force replace u
	replace v5 = v5 + " \\" if _n<5

	*Export to TeX
	listtex * using "$results/table1b.tex", replace ///
		rstyle(tabular) ///
		head("\begin{tabular}{l c c c c c c}" /// number of L or R justified columns in the table
		"\toprule" /// thick line at top of the table
		"       & (1)   		& (2)		& (3)		& (4)           & (5)  \\" ///
		"Share	& Public        	& Public   	& Private       & Large Priv  	& Small Priv \\" ///
		"C-Corp & Multinational 	& Domestic 	& Multinational & Domestic 	& Domestic \\ \midrule") /// 
			foot(   "\bottomrule" /// /// thick line at bottom of the table
			"\end{tabular}")
restore
